<template>
  <div>
    <div class="top">
      <div class="header">
        当前城市 - <i class="iconfont icon-guanbi-01" @click="back"></i>
      </div>
      <div class="footer">
        <div class="sousuo">
          <i class="iconfont icon-fangdajing"></i>
          <input type="text" placeholder="请输入城市名或拼音" />
        </div>
      </div>
    </div>
    <div :class="{ hotCity }" v-show="hotCity">
      <p>热门城市</p>
      <van-grid>
        <van-grid-item
          v-for="item in hot"
          :key="item.cityId"
          :text="item.name"
        />
      </van-grid>
    </div>
    <van-index-bar :index-list="indexList" class="box">
      <div v-for="(item, key) in cityList" :key="key">
        <van-index-anchor :index="key">{{ key }}</van-index-anchor>
        <van-cell
          v-for="v in item"
          :key="v.cityId"
          :title="v.name"
          @click="cityName(v.name, v.cityId)"
        />
      </div>
    </van-index-bar>
  </div>
</template>

<script>
import api from '@/api/index.js'
export default {
  data() {
    return {
      hotCity: true,
      indexList: [], // 放右边的索引列表
      cityList: {}, // 存放城市信息的对象{A:[{a开头的城市1},{a开头的城市1}]}
      words: [], // 放26个英文字母
      hot: []
    }
  },
  methods: {
    cityName(city, cityId) {
      this.$store.commit('city', city)
      this.$store.commit('cityId', cityId)
      this.$router.back()
    },
    back() {
      this.$router.go(-1)
    }
  },
  mounted() {
    window.addEventListener('scroll', () => {
      const t = document.documentElement.scrollTop || document.body.scrollTop
      if (t >= 220) {
        this.hotCity = false
      } else {
        this.hotCity = true
      }
    })
  },
  created() {
    // 将26个英文字母放入words
    for (let i = 65; i <= 90; i++) {
      this.words.push(String.fromCharCode(i))
    }
    this.$http.get(api.getCity).then((res) => {
      console.log(res)
      // 在26个英文字母中筛选需要的城市首拼，放入右侧列表中
      this.words.forEach((word) => {
        res.data.cities.forEach((item) => {
          if (item.isHot === 1) {
            this.hot.push(item)
          }
          if (item.pinyin[0].toUpperCase() === word) {
            // 判断索引列表中是否存在当前word中的字符，没有则放入
            if (this.indexList.indexOf(word) < 0) {
              // 没有
              this.indexList.push(word)
            }
            // 如果当前城市列表中不存在当前字符的城市，定义为空数组
            if (!this.cityList[word]) {
              this.cityList[word] = []
            }
            this.cityList[word].push(item)
          }
        })
      })
      this.hot = this.hot.slice(0, 4)
      console.log(this.hot)
    })
  }
}
</script>

<style lang='scss' scoped>
.top {
  width: 100%;
  height: 100px;
  text-align: center;
  position: fixed;
  left: 0;
  top: 0;
  background-color: white;
  z-index: 2;
  .header {
    width: 100%;
    height: 50px;
    line-height: 50px;
    font-size: 20px;
    i {
      font-size: 12px;
      position: absolute;
      left: 10px;
      top: 0;
    }
  }
  .footer {
    width: 100%;
    height: 50px;
    background-color: #ccc;
    padding-top: 5px;
    .sousuo {
      width: 80%;
      height: 40px;
      margin: auto;
      border: 1px solid #ccc;
      background-color: white;
      input {
        outline: none;
        border: 0;
        height: 38px;
      }
      i {
        font-size: 14px;
      }
    }
  }
}
.hotCity {
  width: 100%;
  height: 100px;
  position: fixed;
  top: 100px;
  left: 0;
  p {
    font-size: 13px;
    color: #797d82;
    margin-bottom: 10px;
    padding-left: 20px;
  }
}
.box {
  padding-top: 200px;
}
</style>
